package main

import (
	"context"
	"fmt"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"grpc-example/protobuf/echo"
	"log"
)

/*
1）首先使用 grpc.Dial() 与 gRPC 服务器建立连接；
2）使用pb.NewGreeterClient(conn)获取客户端；
3）通过客户端调用ServiceAPI方法client.SayHello
*/
const (
	address = "localhost:50051"
)

func main() {
	// 1.与server建立连接
	conn, err := grpc.Dial(address, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock())
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	// 2.获取客户端
	c := echo.NewEchoClient(conn)
	// 3.执行各个Stream的对应方法
	unary(c)
}
func unary(client echo.EchoClient) {
	resp, err := client.UnaryEcho(context.Background(), &echo.EchoRequest{Message: "hello world"})
	if err != nil {
		log.Printf("send error:%v\n", err)
	}
	fmt.Printf("Recved:%v \n", resp.GetMessage())
}
